package com.ruoyi.heytalk.controller.system;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.heytalk.CommonConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.heytalk.ImApply;
import com.ruoyi.common.core.domain.heytalk.dto.AddFriendDTO;
import com.ruoyi.common.core.domain.heytalk.dto.HandleFriendDTO;
import com.ruoyi.common.core.domain.heytalk.dto.SearchUserDTO;
import com.ruoyi.common.core.domain.heytalk.vo.SearchUserVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.heytalk.service.IImApplyService;
import com.ruoyi.heytalk.service.IImUserService;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 通用请求控制层
 * @author zhusy
 * @since 2021/10/31
 */
@Api(value = "通用请求控制层", tags = {"通用请求控制层"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping(CommonConstants.HEY_TALK)
public class CommonController extends BaseController {

    private final IImUserService userService;

    private final IImApplyService applyService;

    private final ISysOssService iSysOssService;

    @ApiOperation("搜索用户")
    @PostMapping("/search/user")
    public AjaxResult<SearchUserVO> searchUser(@Validated @RequestBody SearchUserDTO searchUserDTO) {
        return userService.searchUser(searchUserDTO);
    }

    @ApiOperation("申请添加好友")
    @PostMapping("/apply/addFriend")
    public AjaxResult<Void> addFriend(@Validated @RequestBody AddFriendDTO addFriendDTO) {
        return applyService.addFriend(addFriendDTO);
    }

    @ApiOperation("获取好友申请列表")
    @GetMapping("/apply/page")
    public TableDataInfo<ImApply> applyPage() {
        return applyService.applyPage();
    }

    @ApiOperation("处理好友申请")
    @PostMapping("/apply/handle")
    public AjaxResult<Void> handleFriend(@Validated @RequestBody HandleFriendDTO handleFriendDTO) {
        return applyService.handleFriend(handleFriendDTO);
    }

    /**
     * 上传OSS对象存储
     */
    @ApiOperation("上传OSS对象存储")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "java.io.File", required = true),
    })
    @RepeatSubmit
    @PostMapping("/upload")
    public AjaxResult<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
        if (Objects.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        System.out.println("im上传图片");
        SysOss oss = iSysOssService.upload(file);
        Map<String, String> map = new HashMap<>(2);
        map.put("url", oss.getUrl());
        map.put("fileName", oss.getFileName());
        return AjaxResult.success(map);
    }

}
